package com.wsz.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.security.pojo.Role;
import com.wsz.security.pojo.User;
import com.wsz.security.pojo.UserRole;
import com.wsz.security.service.RoleService;
import com.wsz.security.service.UserRoleService;
import com.wsz.security.service.UserService;
import com.wsz.security.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangs
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2022-11-17 16:33:11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService{

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<Role> getRolesByUid(Integer id) {
        List<UserRole> userRoles = userRoleService.list(new QueryWrapper<UserRole>().eq("uid", id));
        List<Integer> roleIds = new ArrayList<>();
        for (int i = 0; i < userRoles.size(); i++) {
            roleIds.add(userRoles.get(i).getRid());
        }

        return  roleService.list(new QueryWrapper<Role>().in("id",roleIds));
    }
}




